package com.hup.utils.swing.screenShot;

import com.hup.utils.commons.hardware.MouseUtil;
import com.hup.utils.commons.hardware.ScreenUtil;
import com.hup.utils.commons.hardware.SystemUtil;
import com.hup.utils.swing.FrameUtil;
import com.hup.utils.swing.FrameUtil.VisibleListener;
import com.hup.utils.swing.screenShot.ScreenShotLabel.LabelCallback;
import com.hup.utils.swing.screenShot.ScreenShotUtil.ShotHandler;
import com.hup.utils.swing.screenShot.ScreenShotUtil.ShotType;
import lombok.extern.log4j.Log4j2;

import javax.swing.*;
import java.awt.*;
import java.lang.ref.WeakReference;

/**
 * [截屏窗口]:能在状态栏上显示的
 */
@Log4j2
class ScreenShotFrame extends JFrame {
    private static WeakReference<ScreenShotFrame> weakReference;

    protected static void show(ShotType type, boolean includeSidebar, ShotHandler handler) {
        ScreenShotFrame ssf;
        if (weakReference == null || weakReference.get() == null) {
            weakReference = new WeakReference<>(new ScreenShotFrame());
        }
        ssf = weakReference.get();
        //noinspection ConstantConditions idea识别错误??!
        if (ssf.isVisible()) ssf.setVisible(false);
        ssf.flush();//处理窗口本身就显示中的情况
        ssf.applyAndShow(type, includeSidebar, handler);
    }

    private final ScreenShotLabel imageLabel;

    private ShotType shotType;
    private boolean includeSidebar;
    private ShotHandler callerHandler;

    private ScreenShotFrame() {
        setUndecorated(true);
        setAlwaysOnTop(true);
        imageLabel = new ScreenShotLabel(new LabelCallback() {
            @Override
            public JFrame getFrame() {
                return ScreenShotFrame.this;
            }

            @Override
            public boolean includeSidebar() {
                return includeSidebar;
            }

            @Override
            public ShotType shotType() {
                return shotType;
            }

            @Override
            public ShotHandler callerHandler() {
                return callerHandler;
            }
        });
        add(imageLabel);
        SystemUtil.disableInputMethods(this);//禁用输入法
        initListener();
    }

    private void initListener() {
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        FrameUtil.addVisibleListener(this, new VisibleListener() {
            @Override
            public void onShown() {}

            @Override
            public void onHidden(boolean dispose) {
                if (dispose) flush();
                //false时,是刷新截屏时隐藏界面
            }
        });

    }

    /**
     * 释放旧对象内存
     */
    private void flush() {
        log.debug("");
        imageLabel.flushAndReset();
        callerHandler = null;
    }

    private void applyAndShow(ShotType type, boolean includeSidebar, ShotHandler handler) {
        this.shotType = type;
        this.includeSidebar = includeSidebar;
        this.callerHandler = handler;
        setTitle(handler.getTitle(type));
        //根据最新的屏幕尺寸设置窗口
        if (includeSidebar) {
            Dimension size = ScreenUtil.getScreenSize();
            setBounds(0, 0, size.width, size.height);
        } else {
            Rectangle bounds = ScreenUtil.getScreenUsableBounds();
            log.debug("bounds={}", bounds);
            setBounds(bounds);
        }

        //刷新屏幕图像
        imageLabel.refreshImage();

        if (type == ShotType.COLOR) {
            setCursor(MouseUtil.getCursor("/static/icon/icon_pipet.gif", "取色光标", max -> new Point(0, max.height -1)));
        } else {
            setCursor(Cursor.getDefaultCursor());
        }
        //setSize(800, 500);//test
        //FrameUtil.setLocation(this, Gravity.MID_R);
        setVisible(true);
    }

}